/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.DisChannel;
import me.zhengjie.modules.system.repository.DisChannelRepository;
import me.zhengjie.modules.system.service.DisChannelService;
import me.zhengjie.modules.system.service.dto.DisChannelDto;
import me.zhengjie.modules.system.service.dto.DisChannelQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.DisChannelMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wangyu
* @date 2024-03-17
**/
@Service
@RequiredArgsConstructor
public class DisChannelServiceImpl implements DisChannelService {

    private final DisChannelRepository disChannelRepository;
    private final DisChannelMapper disChannelMapper;

    @Override
    public Map<String,Object> queryAll(DisChannelQueryCriteria criteria, Pageable pageable){
        Page<DisChannel> page = disChannelRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(disChannelMapper::toDto));
    }

    @Override
    public List<DisChannelDto> queryAll(DisChannelQueryCriteria criteria){
        return disChannelMapper.toDto(disChannelRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DisChannelDto findById(Long channelId) {
        DisChannel disChannel = disChannelRepository.findById(channelId).orElseGet(DisChannel::new);
        ValidationUtil.isNull(disChannel.getChannelId(),"DisChannel","channelId",channelId);
        return disChannelMapper.toDto(disChannel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DisChannelDto create(DisChannel resources) {
        return disChannelMapper.toDto(disChannelRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DisChannel resources) {
        DisChannel disChannel = disChannelRepository.findById(resources.getChannelId()).orElseGet(DisChannel::new);
        ValidationUtil.isNull( disChannel.getChannelId(),"DisChannel","id",resources.getChannelId());
        disChannel.copy(resources);
        disChannelRepository.save(disChannel);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long channelId : ids) {
            disChannelRepository.deleteById(channelId);
        }
    }

    @Override
    public void download(List<DisChannelDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DisChannelDto disChannel : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("渠道名称", disChannel.getChannelName());
            map.put("渠道编码", disChannel.getChannelCode());
            map.put("管理员手机号", disChannel.getAdminMobile());
            map.put("管理员真实姓名", disChannel.getAdminName());
            map.put("管理员昵称", disChannel.getAdminNickname());
            map.put("创建时间", disChannel.getCreateTime());
            map.put("创建人", disChannel.getCreateby());
            map.put("更新时间", disChannel.getUpdateTime());
            map.put("更新人", disChannel.getUpdateby());
            map.put("状态0：无效，1:有效，-1：删除", disChannel.getChannelStatus());
            map.put("管理员openid", disChannel.getAdminOpenid());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}